<template>
  <div class="table-editor">
    <el-form :model="localTableData" label-width="100px" size="small">
      <!-- 表头设置 -->
      <el-form-item label="表头设置">
        <el-button type="primary" size="mini" @click="addHeader">添加表头</el-button>
        <div v-for="(header, index) in localTableData.headers" :key="index" class="header-item">
          <el-input
            v-model="localTableData.headers[index]"
            :placeholder="`表头 ${index + 1}`"
            style="width: 200px; margin-right: 10px;"
          />
          <el-button size="mini" type="danger" @click="removeHeader(index)">删除</el-button>
        </div>
      </el-form-item>

      <!-- 数据行编辑 -->
      <el-form-item label="数据行">
        <el-button type="primary" size="mini" @click="addRow">添加行</el-button>
        <div class="table-data-editor">
          <div v-for="(row, rowIndex) in localTableData.data" :key="rowIndex" class="data-row">
            <span class="row-label">行 {{ rowIndex + 1 }}:</span>
            <div class="row-cells">
              <el-input
                v-for="(header, colIndex) in localTableData.headers"
                :key="colIndex"
                v-model="row[`col${colIndex}`]"
                :placeholder="header"
                size="mini"
                style="width: 120px; margin: 0 5px 5px 0;"
              />
            </div>
            <el-button size="mini" type="danger" @click="removeRow(rowIndex)">删除</el-button>
          </div>
        </div>
      </el-form-item>

      <!-- 表格设置 -->
      <el-form-item label="显示设置">
        <el-checkbox v-model="localTableData.showHeader">显示表头</el-checkbox>
        <el-checkbox v-model="localTableData.border">显示边框</el-checkbox>
      </el-form-item>
    </el-form>

    <div class="editor-actions">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="save">保存</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TableEditor',
  props: {
    tableData: {
      type: Object,
      default: () => ({
        headers: ['参数', '数值', '单位'],
        data: [],
        showHeader: true,
        border: true
      })
    }
  },
  data() {
    return {
      localTableData: JSON.parse(JSON.stringify(this.tableData))
    }
  },
  methods: {
    addHeader() {
      this.localTableData.headers.push(`列${this.localTableData.headers.length + 1}`)
      // 为所有现有行添加新列的占位符
      this.localTableData.data.forEach(row => {
        row[`col${this.localTableData.headers.length - 1}`] = ''
      })
    },
    
    removeHeader(index) {
      if (this.localTableData.headers.length <= 1) {
        this.$message.warning('至少保留一个表头')
        return
      }
      this.localTableData.headers.splice(index, 1)
      // 移除所有行中对应的列数据
      this.localTableData.data.forEach(row => {
        delete row[`col${index}`]
        // 重新排列列索引
        for (let i = index; i < this.localTableData.headers.length; i++) {
          row[`col${i}`] = row[`col${i + 1}`] || ''
          if (i === this.localTableData.headers.length - 1) {
            delete row[`col${i + 1}`]
          }
        }
      })
    },
    
    addRow() {
      const newRow = {}
      this.localTableData.headers.forEach((_, index) => {
        newRow[`col${index}`] = ''
      })
      this.localTableData.data.push(newRow)
    },
    
    removeRow(index) {
      this.localTableData.data.splice(index, 1)
    },
    
    save() {
      this.$emit('save', this.localTableData)
    },
    
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>

<style scoped>
.table-editor {
  padding: 20px;
}

.header-item {
  margin: 8px 0;
  display: flex;
  align-items: center;
}

.table-data-editor {
  margin-top: 10px;
  max-height: 400px;
  overflow-y: auto;
}

.data-row {
  display: flex;
  align-items: flex-start;
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #e6e8eb;
  border-radius: 4px;
}

.row-label {
  min-width: 60px;
  font-weight: 500;
  margin-right: 10px;
}

.row-cells {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
}

.editor-actions {
  text-align: right;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e6e8eb;
}
</style>